<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:user="http://ionrock.org/ns/wsgiappclient/user"
                xmlns:app="http://purl.org/atom/app#"
                xmlns:a="http://www.w3.org/2005/Atom"
                xmlns:ac="http://ionrock.org/ns/app-client"
                xmlns:dyn="http://exslt.org/dynamic"
                xmlns:f="http://xmlns.4suite.org/ext" 
                xmlns:xh="http://www.w3.org/1999/xhtml"
                xmlns:ion="http://ionrock.org/ns/http-document"
                xmlns:exsl="http://exslt.org/common"
                xmlns:func="http://exslt.org/functions"
                xmlns="http://www.w3.org/1999/xhtml"
                extension-element-prefixes="dyn f ion func"
                exclude-result-prefixes="ac xh #default dyn f ion">

  <xsl:output method="html" omit-xml-declaration="yes" indent="yes" />
  <xsl:param name="ac:base_url" />
  <xsl:param name="ac:openid" />

  <xsl:param name="ac:service" />
  <xsl:param name="ac:username" />
  <xsl:param name="ac:password" />
  <xsl:param name="ac:xml_base" />
  <xsl:param name="ac:location" />
  <xsl:param name="ac:flash" />
  <xsl:param name="ac:form_action">.</xsl:param>
  <xsl:param name="ac:check_params">Nope not working</xsl:param>


  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Atom Publishing Protocol Client</title>
        <xsl:variable name="css" select="f:resolve-url($ac:base_url, 'static/css/styles.css')" />
        <link rel="stylesheet" type="text/css" href="{$css}" />
      </head>
      <body>
        <div id="content-block">
          <div id="header-block">
            <h4>WSGIAppClient</h4>
            <div class="subheading">An Atom Publishing Protocol Client</div>
            <div class="breadcrumbs">
              <a href="{ac:this-base-url()}">home</a>
            </div>
          </div>
          <div id="body-block">
            <xsl:if test="$ac:flash">
              <div class="flash-message">
                <xsl:value-of select="$ac:flash" />
              </div>
            </xsl:if>
            <div class="author_info">
              <form action="{ac:this-base-url('author')}" method="post">
                <fieldset class="author-info">
                  <legend>Author Info</legend>
                  <xsl:apply-templates select="/user:user/a:author" mode="author-forms" />
                  <input type="submit" name="submit" value="update" />
                </fieldset>
              </form>
            </div>
            <div class="service_list">
              <fieldset class="user-collection">
                <legend>My Collections</legend>
                <xsl:apply-templates />
              </fieldset>
            </div>
            <div class="add-service-forms">
              <form action="{ac:this-base-url()}" method="POST">
                <fieldset>
                  <legend>Add Service</legend>
                  <input type="hidden" name="action" value="service-get-collections" />
                  <div class="text-input">
                    <label class="text" for="app_service">Service URL</label>
                    <input type="text" name="app_service" value="{$ac:service}" />
                  </div>
                  <div class="text-input">
                    <label class="text" for="username">Username</label>
                    <input type="text" name="username" value="{$ac:username}" />
                  </div>
                  <div class="text-input">
                    <label class="text" for="password">Password</label>
                    <input type="password" name="password" value="{$ac:password}" />
                  </div>
                  <input type="submit" name="submit" value="add" />
                </fieldset>
              </form>
            </div>
            <xsl:if test="$ac:service">
              <xsl:variable name="service-document" 
                            select="exsl:node-set(
                                      ac:load-http-doc(
                                        $ac:service, 
                                        $ac:username, 
                                        $ac:password))" />
              <xsl:if test="$service-document">
                <xsl:apply-templates select="$service-document//app:workspace" />
              </xsl:if>
            </xsl:if>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="text()" />

  <xsl:template match="a:author" mode="author-forms">
    <label for="atom_author_name">Name</label>
    <input type="text" name="atom_author_name" value="{./a:name}" />
    <label for="atom_author_email">Email</label>
    <input type="text" name="atom_author_email" value="{./a:email}" />
    <label for="atom_author_uri">URI</label>
    <input type="text" name="atom_author_uri" value="{./a:uri}" />
  </xsl:template>

  <xsl:template match="user:collection">
    <xsl:variable name="collection-xml" select="ac:load-http-doc(./@href, ./@username, ./@password)" />
    <xsl:if test="$collection-xml">
      <xsl:variable name="collection" select="exsl:node-set($collection-xml)" />
      <xsl:if test="$collection">
        <xsl:apply-templates select="$collection//a:feed" mode="collection-forms">
          <xsl:with-param name="url" select="./@href" />
        </xsl:apply-templates>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template match="app:workspace">
    <fieldset>
      <legend>Add Collections</legend>
      <form action="{ac:this-base-url()}" method="post">
        <input type="hidden" name="action" value="user-add-collections" />
        <input type="hidden" name="username" value="{$ac:username}" />
        <input type="hidden" name="password" value="{$ac:password}" />
        <table>
        <xsl:for-each select="./app:collection">
          <tr>
            <xsl:variable name="url" select="./@href" />
            <xsl:variable name="title" select="./a:title" />
            <td><a href="{$url}"><xsl:value-of select="$title" /></a></td>
            <td><input type="checkbox" name="collection_urls" value="{$url}" /></td>
          </tr>
        </xsl:for-each>
        </table>
        <input type="submit" name="submit" value="Add" />
      </form>
    </fieldset>
  </xsl:template>

  <xsl:template match="a:feed" mode="collection-forms">
    <xsl:param name="url" />
    <div class="collection">
      <h3><xsl:value-of select="./a:title" /></h3>
      <form action="{ac:this-base-url('edit')}" method="post">
        <input type="hidden" name="action" value="default" />
        <input type="hidden" name="collection_uri" value="{$url}" />
        <select name="entry_uri">
          <xsl:apply-templates select="//a:entry" />
        </select>
        <input type="submit" name="submit" value="edit" />
      </form>
      <a href="./create/?collection_uri={$url}">Create Entry</a> |
      <a href="./collection/delete/?collection_uri={$url}">Delete Collection</a> 
    </div>
  </xsl:template>

  <xsl:template match="a:entry">
    <xsl:for-each select="./a:link[@rel = 'edit']">
      <!-- we assume the URL either has an xml:base or is already a full path -->
      <xsl:variable name="edit-href">
        <xsl:variable name="base" select="f:base-uri(.)" />
        <xsl:choose>
          <xsl:when test="$base">
            <xsl:value-of select="f:resolve-url($base, ./@href)" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="./@href" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <option value="{$edit-href}">
        <xsl:value-of select="../a:title" />
      </option>
    </xsl:for-each>
  </xsl:template>
  
  <func:function name="ac:this-base-url">
    <xsl:param name="path" />
    <func:result select="f:resolve-url(f:resolve-url($ac:base_url, concat($ac:openid, '/')), $path)" />
  </func:function>

  <func:function name="ac:load-http-doc">
    <xsl:param name="url" />
    <xsl:param name="u" />
    <xsl:param name="p" />
    <func:result>
      <xsl:if test="$url">
        <xsl:variable name="http-doc" select="ion:get-http-document($url, $u, $p)" />
        <xsl:if test="string-length($http-doc) &gt; 0">
         <xsl:message>[<xsl:value-of select="$http-doc" />]</xsl:message>
          <xsl:variable name="doc" select="f:parse-xml($http-doc)" />
          <xsl:if test="$doc">
            <xsl:copy-of select="$doc" />
          </xsl:if>
        </xsl:if>
      </xsl:if>
    </func:result>
  </func:function>

</xsl:stylesheet>